package com.mryang.hds.controller;


import com.mryang.hds.entity.Need;
import com.mryang.hds.entity.PunchRecord;
import com.mryang.hds.entity.TripInfo;
import com.mryang.hds.entity.User;
import com.mryang.hds.service.INeedService;
import com.mryang.hds.service.ITripInfoService;
import com.mryang.hds.service.IUserService;
import com.mryang.hds.utils.JWTUtil;
import com.mryang.hds.utils.Md5Util;
import com.mryang.hds.utils.ResultJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author Genius
 * @version 1.0.0
 * @ClassName UserController.java
 * @Description TODO 用户控制器
 * @createTime 2022年12月24日 14:07:00
 */

@RequestMapping("/api/home")
@RestController
public class UserController {

    @Autowired
    IUserService iUserService;

    @Autowired
    ITripInfoService iTripInfoService;

    @Autowired
    INeedService iNeedService;


    /**
     * 获取已登录用户信息
     * @param httpServletRequest 获取header中的token信息
     * @return 用户部分信息
     */
    @GetMapping("/getUser")
    public ResultJson getUser(HttpServletRequest httpServletRequest){

        // 从token中解析出用户编号
        String token = httpServletRequest.getHeader("token");
        Integer uid = JWTUtil.getTokenInfoIndex(token);

        // 获取用户信息
        User user = iUserService.getUserById(uid);

        if (user == null){
            return ResultJson.error("该用户信息为空！");
        }

        return ResultJson.success(user,"用户信息查找成功");

    }

    /**
     * 修改密码
     * @param oldPwd 旧密码
     * @param newPwd 新密码
     * @param httpServletRequest 获取登录用户信息
     * @return
     */
    @PostMapping("updatePwd")
    public ResultJson updatePwd(@RequestParam("oldPwd") String oldPwd,
                                @RequestParam("newPwd") String newPwd,
                                HttpServletRequest httpServletRequest){

        String token = httpServletRequest.getHeader("token");
        Integer uid = JWTUtil.getTokenInfoIndex(token);

        User user = iUserService.getUserById(uid);

        // 用户输入的旧密码正确，更新密码
        if (user.getPassword().equals(Md5Util.encode(oldPwd))){
            newPwd = Md5Util.encode(newPwd);
            int i = iUserService.updatePwd(uid, newPwd);

            if (i > 0){
                return ResultJson.success("密码修改成功");
            }

        }else{
            return ResultJson.error("原始密码输入错误");
        }


        return ResultJson.error("密码修改失败");

    }


    /**
     * 个人中心相关信息统计
     * @param httpServletRequest
     * @return 统计结果
     */
    @GetMapping("/usercenter/aboutme")
    public ResultJson aboutMe(HttpServletRequest httpServletRequest){
        Integer userId = JWTUtil.getTokenInfoIndex(httpServletRequest.getHeader("token"));
        Map<String, Integer> stringIntegerMap = iUserService.aboutMe(userId);

        return ResultJson.success(stringIntegerMap);
    }


    /**
     * 获取用户自隔离期以来的所有打卡情况
     * @param httpServletRequest
     * @return
     */
    @GetMapping("/usercenter/getpr")
    public ResultJson getPR(HttpServletRequest httpServletRequest){
        Integer uid = JWTUtil.getTokenInfoIndex(httpServletRequest.getHeader("token"));

        List<PunchRecord> punchRecordById = iUserService.getPunchRecordById(uid);

        return ResultJson.success(punchRecordById);
    }

    /**
     * 获取用户行程信息
     * @param httpServletRequest
     * @return
     */
    @GetMapping("/usercenter/gettrips")
    public ResultJson getTrips(HttpServletRequest httpServletRequest){
        Integer uid = JWTUtil.getTokenInfoIndex(httpServletRequest.getHeader("token"));

        List<TripInfo> tripsByUId = iTripInfoService.getTripsByUId(uid);

        return ResultJson.success(tripsByUId);
    }

    /**
     * 获取用户需求信息
     * @param httpServletRequest
     * @return
     */
    @GetMapping("/usercenter/getneeds")
    public ResultJson getNeeds(HttpServletRequest httpServletRequest){
        Integer uid = JWTUtil.getTokenInfoIndex(httpServletRequest.getHeader("token"));

        List<Need> needsByUId = iNeedService.getNeedsByUId(uid);

        return ResultJson.success(needsByUId);
    }

    /**
     * 修改需求状态
     * @param needId 需求编号
     * @return
     */
    @PutMapping("/usercenter/editneedstate/{needId}")
    public ResultJson editNeedState(@PathVariable Integer needId){

        iNeedService.editNeedState(needId);

        return ResultJson.success("需求状态修改成功");
    }
}
